/*
 * Principal.java
 *
 * Created on 6 de octubre de 2008, 10:22 PM
 */
package org.javahispano.javacup.gui.principal;

import java.awt.Component;
import java.awt.Graphics;
import javax.swing.JList;
import org.javahispano.javacup.visores.VisorOpenGl;
import org.javahispano.javacup.visores.VisorBasico;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.thoughtworks.xstream.XStream;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import org.javahispano.javacup.modelo.Constantes;
import org.javahispano.javacup.modelo.Partido;
import org.javahispano.javacup.modelo.PartidoGuardado;
import org.javahispano.javacup.modelo.PartidoInterfaz;
import org.javahispano.javacup.modelo.Tactica;
import org.javahispano.javacup.modelo.TacticaDetalle;

/**Aplicacion que permite visualizar, grabar, repetir y configurar partidos, uso interno*/
public final class Principal extends javax.swing.JFrame implements Runnable {

    private static final long serialVersionUID = 1L;
    private XStream xs = new XStream();
    PrincipalDatos datos = new PrincipalDatos();
    private DefaultListModel guardados = new DefaultListModel();
    private final DefaultListModel tacticasModel = new DefaultListModel();
    private JFileChooser jfc;
    private Class tl = null, tv = null;
    private ArrayList<File> filedirs = new ArrayList<File>();

    /**Busca tacticas en el classpath*/
    void scanForTactics(List<File> dir) {
        tacticasModel.clear();
        filedirs.clear();
        for (File f : dir) {
            try {
                scanDir(f);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    void scanDir(File dir) throws Exception {
        Class[] classes = new Class[]{};
        try {
            classes = getClases(dir, "", Tactica.class, true);
        } catch (Exception e) {
            classes = new Class[]{};
        }
        for (Class c : classes) {
            if (!c.getName().equals("org.javahispano.javacup.modelo.TacticaImpl")
                    && !c.getName().equals("org.javahispano.javacup.gui.asistente.Asistente$TacticaImpl")
                    && !c.getName().equals("org.javahispano.javacup.tacticas_aceptadas.DaniP.Termineitor$JugadaEnsayadaAvanzayPasa")) {
                synchronized (tacticasModel) {
                    tacticasModel.addElement(c);
                    filedirs.add(dir);
                }
            }
        }
        tacticas.repaint();
        tacticas.updateUI();
    }

    private String getClassName(String name) {
        if (name == null) {
            return name;
        }
        int idx = name.lastIndexOf(".");
        if (idx > -1) {
            return name.substring(idx + 1);
        }
        return name;
    }

    /**Actualiza la tactica local y la tactica visita*/
    void updateLocalVisita() {
        if (datos.local != null) {
            jLabel8.setText(getClassName(datos.local));
        }
        if (datos.visita != null) {
            jLabel9.setText(getClassName(datos.visita));
        }
    }

    void setEnableSlickButtons(boolean enable) {
        jButton7.setEnabled(enable);
    }
    LinkedList<File> directorios = new LinkedList<File>();
    final HashMap<Class, TacticaDetalle> tactics = new HashMap<Class, TacticaDetalle>();

    /** Creates new form Principal */
    public Principal() throws Exception {
        initComponents();

        final Font f = new Font("dialog", 1, 18);
        final Font f2 = new Font("arial", 0, 12);
        Image i1 = null;
        Image i2 = null;

        i1 = new ImageIcon(getClass().getResource("/imagenes/minipasto.png")).getImage();
        i2 = new ImageIcon(getClass().getResource("/imagenes/minipasto2.png")).getImage();

        final Image img1 = i1;
        final Image img2 = i2;
        final Dimension dim = new Dimension(0, 33);
        tacticas.setCellRenderer(new ListCellRenderer() {

            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                Class tacticaClass = (Class) value;
                Object instance = null;
                @SuppressWarnings("element-type-mismatch")
                TacticaDetalle d = tactics.get(value);
                if (d == null) {
                    try {
                        instance = tacticaClass.getConstructor(new Class[]{}).newInstance(new Object[]{});
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    Tactica t = (Tactica) instance;
                    d = t.getDetalle();
                    tactics.put(tacticaClass, d);
                }
                final String nombre = d.getNombre();
                final String otros = "clase(" + tacticaClass.getCanonicalName() + ") pais(" + d.getPais() + ") entrenador(" + d.getEntrenador() + ")";
                final Color camiseta = d.getColorCamiseta();
                final Color pantalones = d.getColorPantalon();
                final boolean seleccionado = isSelected;
                final boolean foco = cellHasFocus;
                JPanel panel = new JPanel() {

                    @Override
                    public void paint(Graphics g) {
                        int cuadros = (getWidth() / 33) + 1;
                        for (int i = 0; i < cuadros; i++) {
                            if (!seleccionado) {
                                g.drawImage(img1, i * 33, 0, null);
                            } else {
                                g.drawImage(img2, i * 33, 0, null);
                            }
                        }
                        g.setFont(f);
                        g.setColor(pantalones);
                        g.drawString(nombre, 5, 17);
                        g.setColor(camiseta);
                        g.drawString(nombre, 4, 16);
                        g.setFont(f2);
                        g.setColor(Color.white);
                        g.drawString(otros, 50, 30);
                    }
                };
                panel.setPreferredSize(dim);
                return panel;
            }
        });

        Image img = new ImageIcon(getClass().getResource("/imagenes/iconos/icon.png")).getImage();
        setIconImage(img);
        this.getContentPane().setBackground(Color.black);
        directorios.add(new File("build" + File.separator + "classes"));
        directorios.add(new File("bin"));
        load();
        updateLocalVisita();
        jfc = new JFileChooser(datos.dir);
        if (datos.ubicacion != null) {
            setBounds(datos.ubicacion);
        }
        jList3.setModel(guardados);
        tacticas.setModel(tacticasModel);
    }

    void load() throws Exception {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(System.getProperty("user.home") + File.separator + "javacup2009" + File.separator + "config.xml");
            datos = (PrincipalDatos) xs.fromXML(fis);
        } catch (Exception ex) {
            datos = new PrincipalDatos();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
        if (datos.guardados == null) {
            datos.guardados = new ArrayList<URL>();
        }
        scanForTactics(directorios);
        fromDatos();
    }

    void save() {
        toDatos();
        FileOutputStream fos = null;

        try {
            File out = new File(System.getProperty("user.home") + File.separator + "javacup2009" + File.separator + "config.xml");
            if (!out.getParentFile().exists()) {
                out.getParentFile().mkdirs();
            }
            fos = new FileOutputStream(out);
            xs.toXML(datos, fos);
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

    private void fromDatos() {
        try {
            jTextField6.setText("" + datos.sx);
        } catch (Exception e) {
        }
        try {
            jTextField7.setText("" + datos.sy);
        } catch (Exception e) {
        }
        try {
            jTextField8.setText("" + datos.escala);
        } catch (Exception e) {
        }
        try {
            jCheckBox4.setSelected(datos.fullscreen);
        } catch (Exception e) {
        }
        try {
            jCheckBox5.setSelected(datos.entrada);
        } catch (Exception e) {
        }
        try {
            jCheckBox3.setSelected(datos.marcador);
        } catch (Exception e) {
        }
        try {
            jCheckBox8.setSelected(datos.guardar);
        } catch (Exception e) {
        }
        try {
            jCheckBox1.setSelected(datos.dobleBuffer);
        } catch (Exception e) {
        }
        try {
            jCheckBox2.setSelected(datos.dibujaJugadores);
        } catch (Exception e) {
        }
        try {
            jComboBox1.setSelectedIndex(datos.visor);
        } catch (Exception e) {
        }
        try {
            jComboBox2.setSelectedIndex(datos.tipoTexto);
        } catch (Exception e) {
        }
        try {
            jComboBox4.setSelectedIndex(datos.estadioIdx);
        } catch (Exception e) {
        }
        try {
            jCheckBox7.setSelected(datos.entorno);
        } catch (Exception e) {
        }
        try {
            jCheckBox6.setSelected(datos.estadio);
        } catch (Exception e) {
        }
        try {
            jCheckBox9.setSelected(datos.numeros);
        } catch (Exception e) {
        }
        try {
            jCheckBox10.setSelected(datos.autoescala);
        } catch (Exception e) {
        }
        try {
            jCheckBox11.setSelected(datos.sonidos);
        } catch (Exception e) {
        }
        try {
            jTabbedPane1.setSelectedIndex(datos.tab);
        } catch (Exception e) {
        }
        try {
            jSlider1.setValue((int) (datos.volumenAmbiente * 100f));
        } catch (Exception e) {
        }
        try {
            jSlider2.setValue((int) (datos.volumenCancha * 100f));
        } catch (Exception e) {
        }
        try {
            this.setBounds(datos.ubicacion);
        } catch (Exception e) {
        }
        try {
            setSize(datos.ubicacion.width, datos.ubicacion.height);
        } catch (Exception e) {
        }
        try {
            setPreferredSize(new Dimension(datos.ubicacion.width, datos.ubicacion.height));
        } catch (Exception e) {
        }
        try {
            setMinimumSize(new Dimension(datos.ubicacion.width, datos.ubicacion.height));
        } catch (Exception e) {
        }
        try {
            setMaximumSize(new Dimension(datos.ubicacion.width, datos.ubicacion.height));
        } catch (Exception e) {
        }
        if (datos.guardados != null) {
            for (URL url : datos.guardados) {
                guardados.addElement(url);
            }
        }
        try {
            jTextField1.setText("" + datos.fps);
        } catch (Exception e) {
        }
        try {
            setVisible(true);
        } catch (Exception e) {
        }
        try {
            setResizable(true);
        } catch (Exception e) {
        }
        try {
            setMinimumSize(new Dimension(0, 0));
        } catch (Exception e) {
        }
        try {
            setMaximumSize(new Dimension(4000, 4000));
        } catch (Exception e) {
        }
        try {
            setResizable(true);
        } catch (Exception e) {
        }
    }

    private void toDatos() {
        try {
            datos.sx = Integer.parseInt(jTextField6.getText().trim());
            jTextField6.setForeground(Color.black);
        } catch (NumberFormatException ex) {
            jTextField6.setForeground(Color.red);
        }
        try {
            datos.sy = Integer.parseInt(jTextField7.getText().trim());
            jTextField7.setForeground(Color.black);
        } catch (NumberFormatException ex) {
            jTextField7.setForeground(Color.red);
        }
        try {
            datos.escala = Double.parseDouble(jTextField8.getText().trim());
            jTextField8.setForeground(Color.black);
        } catch (NumberFormatException ex) {
            jTextField8.setForeground(Color.red);
        }
        try {
            datos.fps = Integer.parseInt(jTextField1.getText().trim());
            jTextField1.setForeground(Color.black);
        } catch (NumberFormatException ex) {
            jTextField1.setForeground(Color.red);
        }
        datos.entrada = jCheckBox5.isSelected();
        datos.marcador = jCheckBox3.isSelected();
        datos.guardar = jCheckBox8.isSelected();
        datos.tipoTexto = jComboBox2.getSelectedIndex();
        datos.visor = jComboBox1.getSelectedIndex();
        datos.fullscreen = jCheckBox4.isSelected();
        datos.entorno = jCheckBox7.isSelected();
        datos.estadio = jCheckBox6.isSelected();
        datos.dobleBuffer = jCheckBox1.isSelected();
        datos.dibujaJugadores = jCheckBox2.isSelected();
        datos.numeros = jCheckBox9.isSelected();
        datos.autoescala = jCheckBox10.isSelected();
        datos.sonidos = jCheckBox11.isSelected();
        datos.ubicacion = this.getBounds();
        datos.tab = jTabbedPane1.getSelectedIndex();
        datos.volumenAmbiente = (float) jSlider1.getValue() / 100f;
        datos.volumenCancha = (float) jSlider2.getValue() / 100f;
        datos.estadioIdx = jComboBox4.getSelectedIndex();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        configVisor = new javax.swing.JDialog();
        jTabbedPane2 = new javax.swing.JTabbedPane();
        jPanel23 = new javax.swing.JPanel();
        jCheckBox5 = new javax.swing.JCheckBox();
        jSlider1 = new javax.swing.JSlider();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jSlider2 = new javax.swing.JSlider();
        jCheckBox8 = new javax.swing.JCheckBox();
        jCheckBox11 = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jPanel7 = new javax.swing.JPanel();
        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();
        jCheckBox9 = new javax.swing.JCheckBox();
        jPanel8 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        jTextField6 = new javax.swing.JTextField();
        jCheckBox3 = new javax.swing.JCheckBox();
        jLabel11 = new javax.swing.JLabel();
        jTextField7 = new javax.swing.JTextField();
        jCheckBox4 = new javax.swing.JCheckBox();
        jCheckBox6 = new javax.swing.JCheckBox();
        jCheckBox7 = new javax.swing.JCheckBox();
        jLabel15 = new javax.swing.JLabel();
        jTextField8 = new javax.swing.JTextField();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jCheckBox10 = new javax.swing.JCheckBox();
        jComboBox4 = new javax.swing.JComboBox();
        jPopupMenu1 = new javax.swing.JPopupMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tacticas = new javax.swing.JList();
        jPanel17 = new javax.swing.JPanel();
        jToolBar3 = new javax.swing.JToolBar();
        jButton6 = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jButton5 = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jPanel21 = new javax.swing.JPanel();
        jPanel22 = new javax.swing.JPanel();
        jPanel26 = new javax.swing.JPanel();
        jComboBox1 = new javax.swing.JComboBox();
        jButton13 = new javax.swing.JButton();
        jLabel12 = new javax.swing.JLabel();
        jButton7 = new javax.swing.JButton();
        jCheckBox12 = new javax.swing.JCheckBox();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jList3 = new javax.swing.JList();
        jToolBar2 = new javax.swing.JToolBar();
        jPanel28 = new javax.swing.JPanel();
        jButton10 = new javax.swing.JButton();
        jPanel27 = new javax.swing.JPanel();
        jPanel25 = new javax.swing.JPanel();
        jComboBox3 = new javax.swing.JComboBox();
        jButton15 = new javax.swing.JButton();
        jPanel30 = new javax.swing.JPanel();
        jButton12 = new javax.swing.JButton();
        jPanel24 = new javax.swing.JPanel();
        jPanel29 = new javax.swing.JPanel();
        jButton14 = new javax.swing.JButton();

        configVisor.setTitle("Configuracion del Visor");
        configVisor.setModal(true);
        configVisor.setResizable(false);

        jTabbedPane2.setFont(new java.awt.Font("Arial", 0, 10));

        jPanel23.setLayout(null);

        jCheckBox5.setFont(new java.awt.Font("Arial", 0, 10));
        jCheckBox5.setText(" Mostrar entrada al estado");
        jPanel23.add(jCheckBox5);
        jCheckBox5.setBounds(10, 30, 147, 21);
        jPanel23.add(jSlider1);
        jSlider1.setBounds(100, 110, 130, 25);

        jLabel13.setFont(new java.awt.Font("Arial", 0, 10));
        jLabel13.setText("Sonido Ambiente");
        jPanel23.add(jLabel13);
        jLabel13.setBounds(10, 110, 90, 20);

        jLabel14.setFont(new java.awt.Font("Arial", 0, 10));
        jLabel14.setText("Sonido Cancha");
        jPanel23.add(jLabel14);
        jLabel14.setBounds(10, 80, 90, 20);
        jPanel23.add(jSlider2);
        jSlider2.setBounds(100, 80, 130, 25);

        jCheckBox8.setFont(new java.awt.Font("Arial", 0, 10));
        jCheckBox8.setText("Guardar Partidos");
        jPanel23.add(jCheckBox8);
        jCheckBox8.setBounds(130, 0, 105, 21);

        jCheckBox11.setFont(new java.awt.Font("Arial", 0, 10));
        jCheckBox11.setText("Sonidos Activados");
        jPanel23.add(jCheckBox11);
        jCheckBox11.setBounds(10, 0, 120, 21);

        jLabel1.setFont(new java.awt.Font("Arial", 0, 10));
        jLabel1.setText("FPS");
        jPanel23.add(jLabel1);
        jLabel1.setBounds(150, 50, 20, 20);
        jPanel23.add(jTextField1);
        jTextField1.setBounds(180, 50, 40, 20);

        jTabbedPane2.addTab("General", jPanel23);

        jPanel7.setLayout(null);

        jCheckBox1.setFont(new java.awt.Font("Arial", 0, 10));
        jCheckBox1.setText("Usar doble buffer");
        jPanel7.add(jCheckBox1);
        jCheckBox1.setBounds(10, 10, 105, 21);

        jCheckBox2.setFont(new java.awt.Font("Arial", 0, 10));
        jCheckBox2.setText("Dibujar jugadores ( no circulos )");
        jPanel7.add(jCheckBox2);
        jCheckBox2.setBounds(10, 30, 175, 21);

        jCheckBox9.setFont(new java.awt.Font("Arial", 0, 10));
        jCheckBox9.setText("Numeros de jugadores");
        jPanel7.add(jCheckBox9);
        jCheckBox9.setBounds(10, 50, 131, 21);

        jTabbedPane2.addTab("Visor Basico", jPanel7);

        jPanel8.setLayout(null);

        jLabel10.setFont(new java.awt.Font("Arial", 0, 10));
        jLabel10.setText("Resolucion : ");
        jPanel8.add(jLabel10);
        jLabel10.setBounds(10, 0, 62, 20);

        jTextField6.setFont(new java.awt.Font("Arial", 0, 10));
        jPanel8.add(jTextField6);
        jTextField6.setBounds(70, 0, 50, 19);

        jCheckBox3.setFont(new java.awt.Font("Arial", 0, 10));
        jCheckBox3.setText("Dibujar Tablero Electronico");
        jPanel8.add(jCheckBox3);
        jCheckBox3.setBounds(10, 90, 157, 20);

        jLabel11.setFont(new java.awt.Font("Arial", 0, 10));
        jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel11.setText("x");
        jPanel8.add(jLabel11);
        jLabel11.setBounds(120, 0, 20, 20);

        jTextField7.setFont(new java.awt.Font("Arial", 0, 10));
        jPanel8.add(jTextField7);
        jTextField7.setBounds(140, 0, 50, 20);

        jCheckBox4.setFont(new java.awt.Font("Arial", 0, 10));
        jCheckBox4.setText("Pantalla Completa");
        jPanel8.add(jCheckBox4);
        jCheckBox4.setBounds(10, 20, 115, 21);

        jCheckBox6.setFont(new java.awt.Font("Arial", 0, 10));
        jCheckBox6.setText("Estadio");
        jPanel8.add(jCheckBox6);
        jCheckBox6.setBounds(10, 70, 61, 21);

        jCheckBox7.setFont(new java.awt.Font("Arial", 0, 10));
        jCheckBox7.setText("Entorno");
        jPanel8.add(jCheckBox7);
        jCheckBox7.setBounds(10, 40, 63, 21);

        jLabel15.setFont(new java.awt.Font("Arial", 0, 10));
        jLabel15.setText("Escala : ");
        jPanel8.add(jLabel15);
        jLabel15.setBounds(90, 40, 41, 20);

        jTextField8.setFont(new java.awt.Font("Arial", 0, 10));
        jPanel8.add(jTextField8);
        jTextField8.setBounds(130, 40, 50, 20);

        jComboBox2.setFont(new java.awt.Font("Arial", 0, 10));
        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Sin Informacion", "Numeros cercanos", "Nombres cercanos", "Numeros", "Nombres" }));
        jPanel8.add(jComboBox2);
        jComboBox2.setBounds(70, 110, 120, 19);

        jLabel16.setFont(new java.awt.Font("Arial", 0, 10));
        jLabel16.setText("Jugadores :");
        jPanel8.add(jLabel16);
        jLabel16.setBounds(10, 110, 60, 20);

        jLabel17.setFont(new java.awt.Font("Arial", 0, 10));
        jLabel17.setText("(Pix/m)");
        jPanel8.add(jLabel17);
        jLabel17.setBounds(180, 40, 33, 20);

        jCheckBox10.setFont(new java.awt.Font("Arial", 0, 10));
        jCheckBox10.setText("auto escala");
        jPanel8.add(jCheckBox10);
        jCheckBox10.setBounds(140, 20, 81, 20);

        jComboBox4.setFont(new java.awt.Font("Arial", 0, 10));
        jComboBox4.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nou Camp", "Bernabeu" }));
        jPanel8.add(jComboBox4);
        jComboBox4.setBounds(80, 70, 120, 20);

        jTabbedPane2.addTab("Visor Open GL", jPanel8);

        configVisor.getContentPane().add(jTabbedPane2, java.awt.BorderLayout.CENTER);

        jMenuItem1.setFont(new java.awt.Font("Arial", 0, 10));
        jMenuItem1.setText("Analisis de rendimiento");
        jPopupMenu1.add(jMenuItem1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Javacup 2011");
        setBackground(new java.awt.Color(153, 153, 255));
        setForeground(new java.awt.Color(102, 102, 102));
        setIconImage(new ImageIcon("recursos/imagenes/iconos/icon.png").getImage());
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        getContentPane().setLayout(new java.awt.CardLayout());

        jTabbedPane1.setBackground(new java.awt.Color(0, 102, 153));
        jTabbedPane1.setForeground(new java.awt.Color(255, 255, 255));
        jTabbedPane1.setFont(new java.awt.Font("Verdana", 1, 11));

        jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.LINE_AXIS));

        jScrollPane1.setBorder(null);

        tacticas.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(102, 102, 102)));
        tacticas.setFont(new java.awt.Font("Verdana", 0, 11));
        tacticas.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tacticas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                tacticasMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(tacticas);

        jPanel1.add(jScrollPane1);

        jPanel17.setMaximumSize(new java.awt.Dimension(120, 2048));
        jPanel17.setLayout(new java.awt.CardLayout());

        jToolBar3.setBackground(javax.swing.UIManager.getDefaults().getColor("Nb.ScrollPane.Border.color"));
        jToolBar3.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        jToolBar3.setFloatable(false);
        jToolBar3.setOrientation(1);
        jToolBar3.setRollover(true);
        jToolBar3.setMaximumSize(new java.awt.Dimension(160, 32925));
        jToolBar3.setMinimumSize(new java.awt.Dimension(160, 152));
        jToolBar3.setPreferredSize(new java.awt.Dimension(160, 25));

        jButton6.setFont(new java.awt.Font("Verdana", 0, 11));
        jButton6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/iconos/visita.png"))); // NOI18N
        jButton6.setText("Tactica Visita");
        jButton6.setFocusable(false);
        jButton6.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton6.setMaximumSize(new java.awt.Dimension(2048, 40));
        jButton6.setMinimumSize(new java.awt.Dimension(0, 40));
        jButton6.setPreferredSize(new java.awt.Dimension(0, 40));
        jButton6.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });
        jToolBar3.add(jButton6);

        jLabel9.setFont(new java.awt.Font("Arial", 0, 10));
        jLabel9.setText(" ");
        jToolBar3.add(jLabel9);

        jButton5.setFont(new java.awt.Font("Verdana", 0, 11));
        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/iconos/local.png"))); // NOI18N
        jButton5.setText("Tactica Local");
        jButton5.setFocusable(false);
        jButton5.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton5.setMaximumSize(new java.awt.Dimension(2048, 40));
        jButton5.setMinimumSize(new java.awt.Dimension(0, 40));
        jButton5.setPreferredSize(new java.awt.Dimension(0, 40));
        jButton5.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jToolBar3.add(jButton5);

        jLabel8.setFont(new java.awt.Font("Arial", 0, 10));
        jLabel8.setText(" ");
        jToolBar3.add(jLabel8);

        jButton1.setFont(new java.awt.Font("Verdana", 0, 11));
        jButton1.setText("Local <-> Visita");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setMaximumSize(new java.awt.Dimension(400, 27));
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jToolBar3.add(jButton1);

        jLabel2.setText(" ");
        jToolBar3.add(jLabel2);

        jPanel21.setBorder(javax.swing.BorderFactory.createLineBorder(javax.swing.UIManager.getDefaults().getColor("Button.background"), 3));
        jPanel21.setMaximumSize(new java.awt.Dimension(2048, 75));
        jPanel21.setMinimumSize(new java.awt.Dimension(0, 100));
        jPanel21.setOpaque(false);
        jPanel21.setPreferredSize(new java.awt.Dimension(1, 24));
        jPanel21.setRequestFocusEnabled(false);
        jPanel21.setLayout(new javax.swing.BoxLayout(jPanel21, javax.swing.BoxLayout.Y_AXIS));

        jPanel22.setLayout(new javax.swing.BoxLayout(jPanel22, javax.swing.BoxLayout.LINE_AXIS));
        jPanel21.add(jPanel22);

        jPanel26.setMaximumSize(new java.awt.Dimension(32810, 23));
        jPanel26.setLayout(new javax.swing.BoxLayout(jPanel26, javax.swing.BoxLayout.LINE_AXIS));

        jComboBox1.setFont(new java.awt.Font("Arial", 0, 10));
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Visor Basico", "Visor OpenGL" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });
        jPanel26.add(jComboBox1);

        jButton13.setFont(new java.awt.Font("Arial", 0, 10));
        jButton13.setText(">");
        jButton13.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton13ActionPerformed(evt);
            }
        });
        jPanel26.add(jButton13);

        jPanel21.add(jPanel26);

        jToolBar3.add(jPanel21);

        jLabel12.setText(" ");
        jLabel12.setMaximumSize(new java.awt.Dimension(3, 2000));
        jToolBar3.add(jLabel12);

        jButton7.setFont(new java.awt.Font("Verdana", 0, 11));
        jButton7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/iconos/icon.png"))); // NOI18N
        jButton7.setText("Ejecutar Partido");
        jButton7.setFocusable(false);
        jButton7.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton7.setMaximumSize(new java.awt.Dimension(2048, 40));
        jButton7.setMinimumSize(new java.awt.Dimension(0, 40));
        jButton7.setPreferredSize(new java.awt.Dimension(0, 40));
        jButton7.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });
        jToolBar3.add(jButton7);

        jCheckBox12.setFont(new java.awt.Font("Arial", 0, 10));
        jCheckBox12.setText("Ejecutar Antes");
        jCheckBox12.setFocusable(false);
        jCheckBox12.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jCheckBox12.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jCheckBox12.setMaximumSize(new java.awt.Dimension(200, 23));
        jCheckBox12.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar3.add(jCheckBox12);

        jPanel17.add(jToolBar3, "card2");

        jPanel1.add(jPanel17);

        jTabbedPane1.addTab("Tacticas", jPanel1);

        jPanel3.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        jPanel3.setLayout(new javax.swing.BoxLayout(jPanel3, javax.swing.BoxLayout.LINE_AXIS));

        jList3.setBackground(new java.awt.Color(51, 51, 51));
        jList3.setFont(new java.awt.Font("Lucida Console", 0, 11));
        jList3.setForeground(new java.awt.Color(204, 204, 204));
        jScrollPane4.setViewportView(jList3);

        jPanel3.add(jScrollPane4);

        jToolBar2.setFloatable(false);
        jToolBar2.setOrientation(1);
        jToolBar2.setRollover(true);
        jToolBar2.setMaximumSize(new java.awt.Dimension(2048, 2048));
        jToolBar2.setMinimumSize(new java.awt.Dimension(80, 104));
        jToolBar2.setPreferredSize(new java.awt.Dimension(128, 422));

        jPanel28.setLayout(new javax.swing.BoxLayout(jPanel28, javax.swing.BoxLayout.LINE_AXIS));

        jButton10.setFont(new java.awt.Font("Arial", 0, 10));
        jButton10.setText("Agregar partidos");
        jButton10.setMaximumSize(new java.awt.Dimension(2048, 25));
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });
        jPanel28.add(jButton10);

        jToolBar2.add(jPanel28);

        jPanel27.setLayout(new javax.swing.BoxLayout(jPanel27, javax.swing.BoxLayout.LINE_AXIS));
        jToolBar2.add(jPanel27);

        jPanel25.setMinimumSize(new java.awt.Dimension(70, 23));
        jPanel25.setPreferredSize(new java.awt.Dimension(80, 20));
        jPanel25.setLayout(new javax.swing.BoxLayout(jPanel25, javax.swing.BoxLayout.LINE_AXIS));

        jComboBox3.setFont(new java.awt.Font("Arial", 0, 10));
        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Visor Basico", "Visor OpenGL" }));
        jComboBox3.setMaximumSize(new java.awt.Dimension(32767, 25));
        jComboBox3.setMinimumSize(new java.awt.Dimension(25, 19));
        jComboBox3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox3ActionPerformed(evt);
            }
        });
        jPanel25.add(jComboBox3);

        jButton15.setFont(new java.awt.Font("Arial", 0, 10));
        jButton15.setText(">");
        jButton15.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton15ActionPerformed(evt);
            }
        });
        jPanel25.add(jButton15);

        jToolBar2.add(jPanel25);

        jPanel30.setLayout(new javax.swing.BoxLayout(jPanel30, javax.swing.BoxLayout.LINE_AXIS));

        jButton12.setFont(new java.awt.Font("Arial", 0, 10));
        jButton12.setText("Ver Partido");
        jButton12.setMaximumSize(new java.awt.Dimension(2048, 25));
        jButton12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton12ActionPerformed(evt);
            }
        });
        jPanel30.add(jButton12);

        jToolBar2.add(jPanel30);

        jPanel24.setMaximumSize(new java.awt.Dimension(10, 32767));
        jPanel24.setMinimumSize(new java.awt.Dimension(0, 10));
        jPanel24.setPreferredSize(new java.awt.Dimension(0, 100));
        jToolBar2.add(jPanel24);

        jPanel29.setLayout(new javax.swing.BoxLayout(jPanel29, javax.swing.BoxLayout.LINE_AXIS));

        jButton14.setFont(new java.awt.Font("Arial", 0, 10));
        jButton14.setText("Eliminar de la lista");
        jButton14.setFocusable(false);
        jButton14.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton14.setMaximumSize(new java.awt.Dimension(2048, 25));
        jButton14.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton14.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton14ActionPerformed(evt);
            }
        });
        jPanel29.add(jButton14);

        jToolBar2.add(jPanel29);

        jPanel3.add(jToolBar2);

        jTabbedPane1.addTab("Guardados", jPanel3);

        getContentPane().add(jTabbedPane1, "card2");

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
    run = false;
    save();
}//GEN-LAST:event_formWindowClosing

    private Class foundClass(String name) {
        for (int i = 0; i < tacticasModel.getSize(); i++) {
            Class c = (Class) tacticasModel.get(i);
            if (c.getName().equals(name)) {
                return c;
            }
        }
        return null;
    }

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
    toDatos();
    try {
        scanForTactics(directorios);
    } catch (Exception e) {
    }
    String msg = "Error Slick, revice la consola";
    try {
        tl = foundClass(datos.local);
    } catch (Exception ex) {
        msg = "No se pudo cargar la tactica local";
    }
    try {
        tv = foundClass(datos.visita);
    } catch (Exception ex) {
        msg = "No se pudo cargar la tactica visita";
    }
    if (tl != null && tv != null) {
        try {
            Tactica tlocal = (Tactica) tl.newInstance(), tvisita = (Tactica) tv.newInstance();
            Partido p = new Partido(tlocal, tvisita, jCheckBox12.isSelected() || jCheckBox8.isSelected());
            if (!datos.entrada) {
                p.inicioRapido();
            }

            int sx = Integer.parseInt(jTextField6.getText().trim());
            int sy = Integer.parseInt(jTextField7.getText().trim());

            if (jComboBox1.getSelectedIndex() == 0) {
                setVisible(false);
                VisorBasico.dibujaJugadores = datos.dibujaJugadores;
                VisorBasico.dobleBuffer = datos.dobleBuffer;
                VisorBasico.numeros = datos.numeros;
                VisorBasico.volumenAmbiente = datos.volumenAmbiente;
                VisorBasico.volumenCancha = datos.volumenCancha;
                VisorBasico.sonidos = datos.sonidos;
                VisorBasico.fps = datos.fps;
                if (jCheckBox12.isSelected()) {
                    while (p.getIteracion() < Constantes.ITERACIONES) {
                        try {
                            p.iterar();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    p.getPartidoGuardado().iterar();
                    new VisorBasico(p.getPartidoGuardado(), this);
                } else {
                    new VisorBasico(p, this);
                }
            } else {
                setVisible(false);
                VisorOpenGl.estadioIdx = datos.estadioIdx;
                VisorOpenGl.setVolumenAmbiente(datos.volumenAmbiente);
                VisorOpenGl.setVolumenCancha(datos.volumenCancha);
                VisorOpenGl.setMarcadorVisible(datos.marcador);
                VisorOpenGl.setEntornoVisible(datos.entorno);
                VisorOpenGl.setEstadioVisible(datos.estadio);
                VisorOpenGl.setAutoescala(datos.autoescala);
                VisorOpenGl.setEscala(datos.escala);
                VisorOpenGl.setFPS(datos.fps);
                VisorOpenGl.setTexto(datos.tipoTexto);
                VisorOpenGl.sonidos = datos.sonidos;
                if (jCheckBox12.isSelected()) {
                    while (p.getIteracion() < Constantes.ITERACIONES) {
                        try {
                            p.iterar();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    p.getPartidoGuardado().iterar();
                    new VisorOpenGl(p.getPartidoGuardado(), sx, sy, jCheckBox4.isSelected(), this);
                } else {
                    new VisorOpenGl(p, sx, sy, jCheckBox4.isSelected(), this);
                }
            }
            msg = "";
        } catch (Exception ex) {
            ex.printStackTrace();
            msg = ex.getMessage();
        }
    } else {
        msg = "Primero debe seleccionar establecer el local y la visita";
    }
    if (msg.length() > 0) {
        JOptionPane.showMessageDialog(this, msg);
    }
}//GEN-LAST:event_jButton7ActionPerformed

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
    if (tacticas.getSelectedIndex() > -1) {
        tv = (Class) tacticas.getSelectedValue();
        datos.visita = tv.getName();
        updateLocalVisita();
    } else {
        JOptionPane.showMessageDialog(this, "Debe seleccionar una tactica");
    }
}//GEN-LAST:event_jButton6ActionPerformed

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
    if (tacticas.getSelectedIndex() > -1) {
        tl = (Class) tacticas.getSelectedValue();
        datos.local = tl.getName();
        updateLocalVisita();
    } else {
        JOptionPane.showMessageDialog(this, "Debe seleccionar una tactica");
    }
}//GEN-LAST:event_jButton5ActionPerformed

private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton13ActionPerformed
    configVisor.setSize(260, 190);
    configVisor.setLocationRelativeTo(this);
    configVisor.setVisible(true);
}//GEN-LAST:event_jButton13ActionPerformed

    /**Agrega un partido guardado a la lista de partidos*/
    public void addGuardadoLocal(File[] files) throws MalformedURLException {
        for (File file : files) {
            try {
                URL url = file.toURI().toURL();
                if (!datos.guardados.contains(url)) {
                    datos.guardados.add(url);
                    guardados.addElement(url);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
    jfc.setMultiSelectionEnabled(true);
    if (jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
        try {
            addGuardadoLocal(jfc.getSelectedFiles());
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        }
    }
    jfc.setMultiSelectionEnabled(false);
}//GEN-LAST:event_jButton10ActionPerformed

    @SuppressWarnings({"deprecation"})
private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton12ActionPerformed
        toDatos();
        final Principal princ = this;
        new Thread(new Runnable() {

            @Override
            public void run() {
                setCursor(new Cursor(Cursor.WAIT_CURSOR));
                int idx = jList3.getSelectedIndex();
                if (idx == -1) {
                    JOptionPane.showMessageDialog(princ, "Seleccione un fichero");
                    setCursor(Cursor.getDefaultCursor());
                    return;
                }
                toDatos();
                URL url = (URL) guardados.get(jList3.getSelectedIndex());

                try {
                    PartidoGuardado p = new PartidoGuardado(url);
                    setCursor(Cursor.getDefaultCursor());
                    p.iterar();
                    int sx = Integer.parseInt(jTextField6.getText().trim());
                    int sy = Integer.parseInt(jTextField7.getText().trim());
                    if (jComboBox1.getSelectedIndex() == 0) {
                        setVisible(false);
                        VisorBasico.dibujaJugadores = datos.dibujaJugadores;
                        VisorBasico.dobleBuffer = datos.dobleBuffer;
                        VisorBasico.numeros = datos.numeros;
                        new VisorBasico(p, princ);
                    } else {
                        setVisible(false);
                        VisorOpenGl.setVolumenAmbiente(datos.volumenAmbiente);
                        VisorOpenGl.setVolumenCancha(datos.volumenCancha);
                        VisorOpenGl.setMarcadorVisible(datos.marcador);
                        VisorOpenGl.setEntornoVisible(datos.entorno);
                        VisorOpenGl.setEstadioVisible(datos.estadio);
                        VisorOpenGl.setEscala(datos.escala);
                        VisorOpenGl.setAutoescala(datos.autoescala);
                        VisorOpenGl.setTexto(datos.tipoTexto);
                        VisorOpenGl.setFPS(datos.fps);
                        new VisorOpenGl(p, sx, sy, jCheckBox4.isSelected(), princ);
                    }

                } catch (Exception ex) {
                    setCursor(Cursor.getDefaultCursor());
                    JOptionPane.showMessageDialog(princ, ex.getMessage(), "Error al descomprimir partido", JOptionPane.ERROR_MESSAGE);
                    Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
        }).start();


}//GEN-LAST:event_jButton12ActionPerformed

private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton15ActionPerformed
    configVisor.setSize(260, 190);
    configVisor.setLocationRelativeTo(this);
    configVisor.setVisible(true);
}//GEN-LAST:event_jButton15ActionPerformed

private void jComboBox3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox3ActionPerformed
    jComboBox1.setSelectedIndex(jComboBox3.getSelectedIndex());
}//GEN-LAST:event_jComboBox3ActionPerformed

private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
    jComboBox3.setSelectedIndex(jComboBox1.getSelectedIndex());
}//GEN-LAST:event_jComboBox1ActionPerformed

private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton14ActionPerformed
    int idx = jList3.getSelectedIndex();
    if (idx > -1) {
        guardados.remove(idx);
        datos.guardados.remove(idx);
    }
}//GEN-LAST:event_jButton14ActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    String tmp = datos.local;
    datos.local = datos.visita;
    datos.visita = tmp;
    updateLocalVisita();
}//GEN-LAST:event_jButton1ActionPerformed

private void tacticasMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tacticasMouseReleased
    if (evt.getButton() == MouseEvent.BUTTON3 && tacticas.getSelectedIndex() > -1) {
        jPopupMenu1.show(tacticas, evt.getX(), evt.getY());
    }
}//GEN-LAST:event_tacticasMouseReleased

    public static void main(String[] args) throws Exception {
        //doLiguilla(80);

        new Principal();
    }

    private static int[][][] doLiguilla(int n) {
        int partidos = n * (n - 1) / 2;
        int fechas = partidos / (n / 2);
        int partidosPorFecha = partidos / fechas;
        //System.out.println(partidos + " partidos, " + fechas + " fechas, " + partidosPorFecha + " partidos por fecha");
        int[][][] tmp = new int[fechas][partidosPorFecha][2];
        ArrayList<int[]> list = new ArrayList<int[]>();
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
                if ((i + j) % 2 == 0) {
                    list.add(new int[]{i, j});
                } else {
                    list.add(new int[]{i, j});
                }
            }
        }
        boolean asignados[] = new boolean[n];
        for (int i = 0; i < fechas; i++) {
            for (int k = 0; k < n; k++) {
                asignados[k] = false;
            }
            for (int j = 0; j < partidosPorFecha; j++) {
                int[] found = null;
                int idx = 0;
                while (found == null && idx < list.size()) {
                    int[] element = list.get(idx);
                    if (!asignados[element[0]] && !asignados[element[1]]) {
                        found = element;
                        list.remove(idx);
                        asignados[found[0]] = true;
                        asignados[found[1]] = true;
                    }
                    idx++;
                }
                if (found == null) {
                    //System.out.println("liguilla no encontrada");
                    return null;
                }
                tmp[i][j][0] = found[0];
                tmp[i][j][1] = found[1];
                //System.out.println("fecha " + i + ", partido " + j + ", juegan " + found[0] + "-" + found[1]);
            }
        }
        return tmp;
    }

    /**Obtiene una lista de clases ubicadas dentro de un paquete que son asignables a una clase especifica*/
    @SuppressWarnings("unchecked")
    Class[] getClases(File src, String paquete, Class claseAsignable, boolean recursivo) throws Exception {
        paquete = paquete.replace('.', '/');

        LinkedList<Class> classes = new LinkedList<Class>();
        if (!paquete.endsWith("//svn")) {
            File directory = null;
            try {
                URLClassLoader cld = URLClassLoader.newInstance(new URL[]{src.toURI().toURL()});
                if (cld == null) {
                    throw new ClassNotFoundException(
                            "Cargador de clases no encontrado.");
                }
                directory = new File(src, paquete);
                if (directory.exists()) {
                    File[] files = directory.listFiles();
                    String name, cls;
                    for (int i = 0; i < files.length; i++) {
                        name = files[i].getName();
                        if (files[i].isFile() && name.endsWith(".class")) {
                            cls = (paquete + '/' + name.substring(0, name.length() - 6)).replace('/', '.');
                            if (cls.startsWith(".")) {
                                cls = cls.substring(1);
                            }
                            Class c = cld.loadClass(cls);
                            if (!c.equals(claseAsignable) && claseAsignable.isAssignableFrom(c)) {
                                classes.add(c);
                            }
                        } else if (files[i].isDirectory()) {
                            cls = paquete + "/" + name;
                            for (Class c : getClases(src, cls, claseAsignable,
                                    recursivo)) {
                                classes.add(c);
                            }
                        }
                    }
                } else {
                    throw new ClassNotFoundException(paquete + " no es un paquete valido.");
                }
            } catch (MalformedURLException ex) {
                throw new MalformedURLException(paquete + " (" + src + ") no es una url valido.");
            } catch (NullPointerException x) {
                throw new ClassNotFoundException(paquete + " (" + directory + ") no es un paquete valido.");
            }
        }
        Class[] classesA = new Class[classes.size()];
        classes.toArray(classesA);
        return classesA;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    javax.swing.JDialog configVisor;
    javax.swing.JButton jButton1;
    javax.swing.JButton jButton10;
    javax.swing.JButton jButton12;
    javax.swing.JButton jButton13;
    javax.swing.JButton jButton14;
    javax.swing.JButton jButton15;
    javax.swing.JButton jButton5;
    javax.swing.JButton jButton6;
    javax.swing.JButton jButton7;
    javax.swing.JCheckBox jCheckBox1;
    javax.swing.JCheckBox jCheckBox10;
    javax.swing.JCheckBox jCheckBox11;
    javax.swing.JCheckBox jCheckBox12;
    javax.swing.JCheckBox jCheckBox2;
    javax.swing.JCheckBox jCheckBox3;
    javax.swing.JCheckBox jCheckBox4;
    javax.swing.JCheckBox jCheckBox5;
    javax.swing.JCheckBox jCheckBox6;
    javax.swing.JCheckBox jCheckBox7;
    javax.swing.JCheckBox jCheckBox8;
    javax.swing.JCheckBox jCheckBox9;
    javax.swing.JComboBox jComboBox1;
    javax.swing.JComboBox jComboBox2;
    javax.swing.JComboBox jComboBox3;
    javax.swing.JComboBox jComboBox4;
    javax.swing.JLabel jLabel1;
    javax.swing.JLabel jLabel10;
    javax.swing.JLabel jLabel11;
    javax.swing.JLabel jLabel12;
    javax.swing.JLabel jLabel13;
    javax.swing.JLabel jLabel14;
    javax.swing.JLabel jLabel15;
    javax.swing.JLabel jLabel16;
    javax.swing.JLabel jLabel17;
    javax.swing.JLabel jLabel2;
    javax.swing.JLabel jLabel8;
    javax.swing.JLabel jLabel9;
    javax.swing.JList jList3;
    javax.swing.JMenuItem jMenuItem1;
    javax.swing.JPanel jPanel1;
    javax.swing.JPanel jPanel17;
    javax.swing.JPanel jPanel21;
    javax.swing.JPanel jPanel22;
    javax.swing.JPanel jPanel23;
    javax.swing.JPanel jPanel24;
    javax.swing.JPanel jPanel25;
    javax.swing.JPanel jPanel26;
    javax.swing.JPanel jPanel27;
    javax.swing.JPanel jPanel28;
    javax.swing.JPanel jPanel29;
    javax.swing.JPanel jPanel3;
    javax.swing.JPanel jPanel30;
    javax.swing.JPanel jPanel7;
    javax.swing.JPanel jPanel8;
    javax.swing.JPopupMenu jPopupMenu1;
    javax.swing.JScrollPane jScrollPane1;
    javax.swing.JScrollPane jScrollPane4;
    javax.swing.JSlider jSlider1;
    javax.swing.JSlider jSlider2;
    javax.swing.JTabbedPane jTabbedPane1;
    javax.swing.JTabbedPane jTabbedPane2;
    javax.swing.JTextField jTextField1;
    javax.swing.JTextField jTextField6;
    javax.swing.JTextField jTextField7;
    javax.swing.JTextField jTextField8;
    javax.swing.JToolBar jToolBar2;
    javax.swing.JToolBar jToolBar3;
    javax.swing.JList tacticas;
    // End of variables declaration//GEN-END:variables
    boolean run = true;

    @Override
    public void run() {
        while (run) {
            tacticas.repaint();
            tacticas.updateUI();
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
            }
        }
    }
}
